
from utils import util
from DecryptLogin import login
from utils import util
import json

def get_headers(uid):
    return {
        'accept': 'application/json, text/plain, */*',
        'accept-encoding': 'gzip, deflate',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'no-cache',
        'pragma': 'no-cache',
        'mweibo-pwa': '1',
        'referer': 'https://m.weibo.cn/profile/{}'.format(uid),
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'x-requested-with': 'XMLHttpRequest',
        'user-agent': util.get_random_useragent(),
    }

def get_weibo_session():
    client = login.Client()
    weibo = client.weibo(reload_history=True)
    infos_return, session = weibo.login('702068049@qq.com', '7521018hao?', 'mobile')
    return session

def get_user_cookie():
    client = login.Client()
    weibo = client.weibo(reload_history=True)
    infos_return, session = weibo.login('702068049@qq.com', '7521018hao?', 'mobile')
    return session.cookies

def get_weibo_st():
    url = 'https://m.weibo.cn/api/config'
    response = util.requests_get(url=url,headers=get_headers('7331627378'),cookies=get_user_cookie())
    if util.check_response_is_ok(response):
        response = json.loads(response.text)
        st = response['data']['st']
        return st
    else:
        return None

def follow_user(uid):
    url = 'https://m.weibo.cn/api/friendships/create'
    params = {'uid':uid,'st':get_weibo_st()}
    cookies = get_user_cookie()
    headers = get_headers(uid)
    session = get_weibo_session()
    response =  util.requests_post(url=url,headers=headers,data=params,cookies=cookies)
    print(response.text)

follow_user('7331627378')


